package com.example.carrierapp.ui.main.waybill.details;

import android.app.Activity;
import android.content.Context;

import com.example.carrierapp.base.BasePresenter;
import com.example.carrierapp.net.ApiMiddleHelper;
import com.example.carrierapp.net.listener.DataCallBackListener;
import com.example.carrierapp.util.LogUtil;
import com.example.carrierapp.viewmodel.viewmodel.WaybillDetailsModel;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Flowable;

public class WaybillDetailsPresenter extends BasePresenter<WaybillDetailsContract.View> implements WaybillDetailsContract.Presenter {
    private Context mContext;

    public WaybillDetailsPresenter(Activity activity) {
        mContext = activity;
    }

    @Override
    public void start() {

    }

    @Override
    public void getWaybillDetail(int id) {
        Map<String, String> biddingMap = new HashMap<>();
        biddingMap.put("id", id + "");
        Flowable<JsonObject> flowable = apiStrores.getWaybillDetails(biddingMap);

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext, flowable, new DataCallBackListener() {

            @Override
            public void onSuccess(String json) {
                LogUtil.d("getWaybillDetail-onSuccess-" + json);
                WaybillDetailsModel bean = new Gson().fromJson(json, new TypeToken<WaybillDetailsModel>() {
                }.getType());
                if (getView() != null) {
                    getView().getWaybillDetailSuccess(bean);
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("getWaybillDetail-onFailure-" + msg);
                if (getView() != null) {

                    getView().getWaybillDetailFailde(msg);
                }
            }
        });
    }
}
